{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to make your own Module and Package ?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%writefile moduleOne.py\n",
    "\n",
    "def printTopTenEven():\n",
    "    for i in range(1,10):\n",
    "        if i%2 == 0 :\n",
    "            print(i)\n",
    "            \n",
    "def printTopTenOdd():\n",
    "    for i in range(1,10):\n",
    "        if i % 2 == 1:\n",
    "            print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import moduleOne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "moduleOne.printTopTenEven()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "moduleOne.printTopTenOdd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "dir(math)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import moduleOne\n",
    "\n",
    "dir(moduleOne)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%writefile moduleTwo.py\n",
    "\n",
    "'''\n",
    "Conditions this program will work on - \n",
    "1. There should be only one char passed in\n",
    "2. it should be eiter small or caps\n",
    "'''\n",
    "def isVowel(char):\n",
    "    if len(char)>1:\n",
    "        return \"Only One Char Allowed\"\n",
    "    \n",
    "    char = char.upper()\n",
    "    if char in [\"A\",\"E\",\"I\",\"O\",\"U\"]:\n",
    "        return \"Its is a vowel\"\n",
    "    else:\n",
    "        return \"Its not a Vowel\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import moduleTwo\n",
    "\n",
    "moduleTwo.isVowel(\"e\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ourPackage import moduleOne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir (moduleOne)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ourPackage import moduleTwo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "moduleTwo.isVowel('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(moduleTwo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Unit test code for ModuleTwo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%writefile testModuleTwo.py\n",
    "import unittest\n",
    "from ourPackage import moduleTwo\n",
    "\n",
    "class TestVowel(unittest.TestCase):\n",
    "    \n",
    "    def testOneSingleLetter(self):\n",
    "        char = \"E\"\n",
    "        result = moduleTwo.isVowel(char)\n",
    "        self.assertEqual(result,\"Its is a vowel\")\n",
    "        \n",
    "    def testOneSingleLetterNotVowel(self):\n",
    "        char = \"r\"\n",
    "        result = moduleTwo.isVowel(char)\n",
    "        self.assertEqual(result,\"Its not a Vowel\")\n",
    "        \n",
    "    def testMultipleChar(self):\n",
    "        char = \"ek\"\n",
    "        result = moduleTwo.isVowel(char)\n",
    "        self.assertEqual(result,\"Only One Char Allowed\")\n",
    "        \n",
    "if __name__ == '__main__':\n",
    "    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "! python testModuleTwo.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import moduleTwo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting moduleTwo.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile moduleTwo.py\n",
    "\n",
    "'''\n",
    "Conditions this program will work on - \n",
    "1. There should be only one char passed in\n",
    "2. it should be eiter small or caps\n",
    "'''\n",
    "def isVowel(char):\n",
    "    if len(char)>1:\n",
    "        return \"Only One Char Allowed\"\n",
    "    \n",
    "    char = char.upper()\n",
    "    if char in [\"A\",\"E\",\"I\",\"O\",\"U\"]:\n",
    "        return \"Its is a vowel\"\n",
    "    else:\n",
    "        return \"Its not a Vowel\"\n",
    "        \n",
    "print(\"Module Two __name__ is printing - \",__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Module Two __name__ is printing -  moduleTwo\n"
     ]
    }
   ],
   "source": [
    "import moduleTwo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting programToRunModuleTwo.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile programToRunModuleTwo.py\n",
    "\n",
    "print(\"Indirect call - \")\n",
    "import moduleTwo\n",
    "\n",
    "print(\"from programToRunModuleTwo.py __name__ is printing - \", __name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__name__ is printing -  moduleTwo\r\n",
      "from programToRunModuleTwo.py __name__ is printing -  __main__\r\n"
     ]
    }
   ],
   "source": [
    "! python programToRunModuleTwo.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decorators "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#basic Functions \n",
    "\n",
    "def hello():\n",
    "    print(\"hey everyone\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#basic Functions \n",
    "\n",
    "def hello(string):\n",
    "    print(\"hey everyone\", string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone Purbayna\n"
     ]
    }
   ],
   "source": [
    "hello(\"Purbayna\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "hello() missing 1 required positional argument: 'string'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-a75d7781aaeb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhello\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: hello() missing 1 required positional argument: 'string'"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#basic Functions \n",
    "\n",
    "def hello(string = \"LetsUpgraders\"):\n",
    "    print(\"hey everyone\", string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone Amisha Gupta\n"
     ]
    }
   ],
   "source": [
    "hello(\"Amisha Gupta\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "basic_coder =  \"Hi am basic Coder\"\n",
    "\n",
    "def hi():\n",
    "    person = \"amit\"\n",
    "    \n",
    "    print(locals())\n",
    "    print(globals()[\"basic_coder\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'person': 'amit'}\n",
      "Hi am basic Coder\n"
     ]
    }
   ],
   "source": [
    "hi()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# can we assing the funcution just variable to anther var. ?\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#basic Functions \n",
    "\n",
    "def hello(string = \"LetsUpgraders\"):\n",
    "    print(\"hey everyone\", string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "namaste = hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "namaste()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "amigo = hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "amigo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey everyone Victor Jr\n"
     ]
    }
   ],
   "source": [
    "amigo(\"Victor Jr\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Can we have a Function within a fuciton ?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def whatspp():\n",
    "    print(\"we are inside whatsap word\")\n",
    "    \n",
    "    hello()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "we are inside whatsap word\n",
      "hey everyone LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "whatspp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#can we define a function inside a funcuitn?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hola():\n",
    "    \n",
    "    print(\"This is inside Hola\")\n",
    "    \n",
    "    def bonjor():\n",
    "        print(\"This is inside Bonjor via Hola\")\n",
    "    \n",
    "    bonjor()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is inside Hola\n",
      "This is inside Bonjor via Hola\n"
     ]
    }
   ],
   "source": [
    "hola()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Can we return a function from a function ?\n",
    "\n",
    "\n",
    "def vankam():\n",
    "    print(\"hey i am Tamil Hello\")\n",
    "    \n",
    "    def namskarm():\n",
    "        print(\"Hi this is Telugu hello\")\n",
    "        \n",
    "    namskarm()\n",
    "    return namskarm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hey i am Tamil Hello\n",
      "Hi this is Telugu hello\n"
     ]
    }
   ],
   "source": [
    "some_var = vankam()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi this is Telugu hello\n"
     ]
    }
   ],
   "source": [
    "some_var()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Can we pass function as A Argument ?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def namaskara(name):\n",
    "    print(\"We are inside Kannad hello- \", name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def konichiwa(some_fun):\n",
    "    print(\"We are inside Kerala Hello \")\n",
    "    some_fun(\"Sai\")\n",
    "    print(some_fun)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are inside Kerala Hello \n",
      "We are inside Kannad hello-  Sai\n",
      "<function namaskara at 0x106594a70>\n"
     ]
    }
   ],
   "source": [
    "konichiwa(namaskara)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "# step 1 - make a small func\n",
    "# step 2 - Make a function inside functio \n",
    "# step 3 - pass a function as arg and get it insde the func\n",
    "# step 4 - merge all the code togther and run it \n",
    "\n",
    "def someFun(arg_func):\n",
    "    def wrap_func():\n",
    "        print(\"i am inside wrap func\")\n",
    "        arg_func()\n",
    "        print(\"I am inside the wrap fun bottom\")\n",
    "    return wrap_func\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "our_gen_pro= someFun(vankam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am inside wrap func\n",
      "hey i am Tamil Hello\n",
      "Hi this is Telugu hello\n",
      "I am inside the wrap fun bottom\n"
     ]
    }
   ],
   "source": [
    "our_gen_pro()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "@someFun\n",
    "def hello():\n",
    "    print(\"This is hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am inside wrap func\n",
      "This is hello\n",
      "I am inside the wrap fun bottom\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getInput(some_func):\n",
    "    \n",
    "    def newFunc():\n",
    "        a = int(input(\"Enter a Number \"))\n",
    "        b = int(input(\"Enter b Number \"))\n",
    "        some_func(a,b)\n",
    "    return newFunc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a Number45\n",
      "Enter b Number67\n",
      "Sum =  112\n"
     ]
    }
   ],
   "source": [
    "@getInput\n",
    "def addition(a,b):\n",
    "    print(\"Sum = \", a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a Number56\n",
      "Enter b Number78\n",
      "Mul -  4368\n"
     ]
    }
   ],
   "source": [
    "@getInput\n",
    "def multiply(a,b):\n",
    "    print(\"Mul - \", a*b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
